//
//  FKSwift.swift
//  ChangeLocation
//
//  Created by Master_K on 2020/7/6.
//  Copyright © 2020 ljb48229. All rights reserved.
//

import Foundation
import UIKit


class FKSwift: NSObject {
    @objc public static func downloadURL(urls:[NSString],completion:@escaping ([NSString]) -> Void) {
        var array1:[URL] = [URL]()
        for urlString in urls {
            if let url = URL(string: String(urlString)) {
                array1.append(url)
            }
        }
        
        downloadURLs(array1) { (results) in
            DispatchQueue.main.async {
                let downloadResults: [NSString] = results.map{ result in
                    switch result {
                    case .text(let text, _, _):
                        return NSString(string: text)
                    default:
                        return NSString(string: "")
                    }
                }
                completion(downloadResults)
            }
        }
    }
    
    @objc static func image(url:NSString,completion:@escaping (UIImage?) -> Void){
        do {
            try         FavIcon.downloadPreferred(String(url),width:1024,height:1024) { (result : IconDownloadResult) in
                switch result{
                case .success(let image):
                    completion(image)
                case .failure(_):
                    completion(nil)
                }
            }
        } catch  {
            completion(nil)
        }
    }
    
}
